package com.rivulet.doj_backend.depend;


import org.apache.commons.lang3.StringUtils;
import org.springframework.context.MessageSource;

import java.util.Locale;

public final class RestResponseBuilder {
    private int code;
    private String message;
    private Object result = null;
    private MessageSource messageSource;

    private RestResponseBuilder() {
    }

    public RestResponseBuilder code(int code) {
        this.code = code;
        return this;
    }

    public RestResponseBuilder message(String message) {
        this.message = message;
        return this;
    }

    public RestResponseBuilder result(Object result) {
        this.result = result;
        return this;
    }

    public RestResponseBuilder messageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
        return this;
    }

    public RestResponse build() {
        return this.build(new Object[0]);
    }

    public RestResponse build(Object[] args) {
        if (this.messageSource != null && StringUtils.isNotBlank(this.message)) {
            this.message = this.messageSource.getMessage(this.message, args, this.message, (Locale)null);
        }

        if (this.result == null) {
            return new RestResponse(this.code, this.message);
        } else {
            RestResultResponse resp = new RestResultResponse(this.code, this.message);
            resp.setResult(this.result);
            return resp;
        }
    }

    public RestResultResponse build3(Object[] args) {
        if (this.messageSource != null && StringUtils.isNotBlank(this.message)) {
            this.message = this.messageSource.getMessage(this.message, args, this.message, (Locale)null);
        }

        if (this.result == null) {
            return new RestResultResponse(this.code, this.message);
        } else {
            RestResultResponse resp = new RestResultResponse(this.code, this.message);
            resp.setResult(this.result);
            return resp;
        }
    }

    public static RestResponseBuilder builder() {
        return new RestResponseBuilder();
    }

    public static RestResponseBuilder builder(Status status) {
        return (new RestResponseBuilder()).code(status.getCode()).message(status.getMessage());
    }
}
